local skel = fk.CreateSkill {
    name = "ling__tiaofu",
}

Fk:loadTranslationTable{
    ["ling__tiaofu"] = "调伏",
    ["@[private]$ling__tiaofu"] = "调伏",
    [":ling__tiaofu"] = "你可以观看游戏外的“影”；一名角色使用一张和“影”的点数、花色或类型相同的牌后，你可以获得一张“影”，然后将此牌作为“影”加入游戏外。",
    ["#ling__tiaofu-ask"] = "调伏：请选择一张“影”获得",
}
local U = require "packages/utility/utility"

Fk:addPoxiMethod{
    name = "ling__tiaofu",
    card_filter = function(to_select, selected, data)
      return #selected==0
    end,
    feasible = function(selected, data)
        return data and #data[2] == 1
      end,
    prompt = function ()
      return "#ling__tiaofu-ask"
    end
  }
local checkifsame = function(cards,tocheckcard)
    for _, cid in ipairs(cards) do
        local card = Fk:getCardById(cid)
        if card.suit == tocheckcard.suit or card.number == tocheckcard.number or card.type == tocheckcard.type then
            return true
        end
    end
    return false
end

skel:addEffect(fk.CardUseFinished, {
    can_trigger = function(self, event, target, player, data)
        if not player:hasSkill(self.name) then return false end
        local shades = table.filter(player.room.void, function(cid)
            local card = Fk:getCardById(cid)
            return card.trueName == "shade"
        end)
        if #shades == 0 then
            return false
        end
        return checkifsame(shades, data.card)
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local shades = table.filter(player.room.void, function(cid)
            local card = Fk:getCardById(cid)
            return card.trueName == "shade"
        end)
        local card = room:askToArrangeCards(player, {
            target = player,
            card_map = {shades},
            skill_name = self.name,
            free_arrange = false,
            poxi_type = "ling__tiaofu",

        })[2]
        room:obtainCard(player, card, false, fk.ReasonPrey, player, self.name)
        room:setCardMark(data.card, skel.name, 1)
        Fk:filterCard(data.card.id, player)
        room:moveCards {
            ids = {data.card.id},
            toArea = Card.Void,
            moveReason = fk.ReasonJustMove,
          }
    end,
})

skel:addEffect(fk.AfterCardsMove, {
    can_refresh= function(self, event, target, player, data)
        if not player:hasSkill(self.name) then return false end
        local room = player.room
        local shades = table.filter(room.void, function(cid)
            local card = Fk:getCardById(cid)
            return card.trueName == "shade" or card:getMark(skel.name) ~= 0
        end)
        local record = U.getPrivateMark(player, "$ling__tiaofu")
        return #shades ~= #record
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local shades = table.filter(room.void, function(cid)
            local card = Fk:getCardById(cid)
            return card.trueName == "shade" or card:getMark(skel.name) ~= 0
        end)
        U.setPrivateMark(player, "$ling__tiaofu", shades)
    end,
})

skel:addEffect("filter", {  
    card_filter = function(self, to_select, player, isJudgeEvent)
      return to_select:getMark(skel.name) ~= 0
    end,
    view_as = function(self, player, to_select)
      local name = "ling__shade"
      return Fk:cloneCard(name, to_select.suit, to_select.number)
    end,
  })
  





return skel